<template>
  <div class="bindCard-wrap">
    <div
      v-loading="loading"
      :element-loading-text="loadingText"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255, 255, 255, .8)"
    >
      <el-form label-width="100px">
        <el-form-item label="银行卡号：">
          <el-input
            v-model="bindCardParams.cardNo"
            placeholder="请输入银行卡号"
            oninput="value=value.replace(/[^\d]/g, '')"
            maxlength="19"
          ></el-input>
          <span class="supportBank" @click="supportBankHandle">支持银行</span>
        </el-form-item>
        <el-form-item label="预留手机号：">
          <el-input
            v-model="binkCardInfo.ystMobile"
            placeholder="请输入预留手机号"
            maxlength="11"
            oninput="value=value.replace(/[^\d]/g, '')"
          ></el-input>
        </el-form-item>

        <!-- 短信验证码 -->
        <el-form-item label="短信验证码：">
          <el-input
            class="code-inp"
            v-model="bindCardParams.code"
            maxlength="6"
            placeholder="请输入验证码"
          ></el-input>
          <span v-if="!downShow" class="code-btn" @click="getBindcardCode">获取验证码</span>
          <span v-else class="code-btn downNum-btn">重新发送（{{downNum + 's'}}）</span>
        </el-form-item>
      </el-form>

      <!-- 解绑/绑定银行卡按钮 -->
      <div>
        <el-button type="primary" @click="_bindBankcard">确认绑定</el-button>
      </div>
    </div>

    <!-- 温馨提示 -->
    <div class="footer">
      <h2>
        <span>*</span>温馨提示
      </h2>
      <p>1.绑定银行卡必须和实名认证信息一致</p>
      <p>2.仅居民身份证实名用户可以绑定</p>
    </div>

    <!-- 支持银行 -->
    <el-dialog
      :visible.sync="supportBankFlag"
      :close-on-click-modal="false"
      :lock-scroll="false"
      width="70%"
      custom-class="supportBank-dialog"
    >
      <span slot="title" class="dialog-title">
        <i></i>
        <span>支持银行</span>
      </span>

      <div class="supportBank-content">
        <div class="supportBank-item" v-for="(item,index) in supportingBanks" :key="index">{{item}}</div>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import {
  checkBankCard,
  getLimitList,
  applybindBankcard,
  bindBankcard,
  applyUnbindBankcard,
  unBindBankcard,
  withdrawBuy,
  rechargeBuy,
  rechargeGetquick,
  rechargeQuickbuy,
  getCompanyCard,
  withdrawBuyCompany,
  resendWithdrawCode,
  confirmWithdrawBuyCompany,
  checkSign,
} from "@/api/rechargeWithDrawal";
import { mapGetters } from "vuex";
export default {
  props: {
    binkCardInfo: {
      type: Object,
    },
  },
  computed: {
    ...mapGetters(["supportingBanks"]),
  },
  data() {
    return {
      loading: false,
      loadingText: "银行卡绑定中",
      supportBankFlag: false,
      downShow: false,
      timer: null,
      downNum: "",
      // 绑定银行卡参数
      bindCardParams: {
        cardNo: "",
        ystMobile: "",
        code: "",
      },
    };
  },
  methods: {
    // 重置
    resetPages() {
      console.log('asdasdas',this.bindCardParams.ystMobile);
      this.bindCardParams.cardNo = "";
      this.bindCardParams.ystMobile = "";
      this.bindCardParams.code = "";
      this.loading = false;
      this.supportBankFlag = false;
      this.downShow = false;
      if (this.timer) {
        clearInterval(this.timer);
      }
    },
    // 确认绑定
    _bindBankcard() {
      if (this.bindCardParams.cardNo == "") {
        this.$message.error("请输入银行卡号");
      } else if (this.bindCardParams.ystMobile == "") {
        this.$message.error("请输入银行卡预留手机号");
      } else if (this.bindCardParams.code == "") {
        this.$message.error("请输入验证码");
      } else {
        this.loading = true;
        bindBankcard(this.bindCardParams)
          .then((res) => {
            console.log("确认绑定银行卡", res);
            if (res.code == 1) {
              this.$message.success("银行卡绑定成功");
              this.$emit("bindCardHandle");
            } else {
              this.bindCardParams.code = "";
              clearInterval(this.timer);
              this.timer = null;
              this.downShow = false;
              this.$message.error(res.msg || "绑定失败");
            }
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    // 获取绑定银行卡验证码
    getBindcardCode() {
      console.log("银行卡信息", this.binkCardInfo);
      this.bindCardParams.ystMobile = this.binkCardInfo.ystMobile;
      if (this.bindCardParams.cardNo == "") {
        this.$message.error("请输入银行卡号");
      } else if (this.bindCardParams.ystMobile == "") {
        this.$message.error("请输入预留手机号");
      } else {
        console.log("获取绑定银行卡验证码参数", this.bindCardParams);
        applybindBankcard(this.bindCardParams).then((res) => {
          // console.log("获取绑定银行卡验证码", res);
          if (res.code == 1) {
            this.countDown();
            this.$message.success("发送成功，请注意查收短信");
          } else {
            this.$message.error(res.msg || "发送失败");
          }
        });
      }
    },
    // 验证码倒计时
    countDown() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.downNum = TIME_COUNT;
        this.downShow = true;
        this.timer = setInterval(() => {
          if (this.downNum > 0 && this.downNum <= TIME_COUNT) {
            this.downNum--;
          } else {
            this.downShow = false;
            clearInterval(this.timer);
            this.timer = null;
          }
        }, 1000);
      }
    },
    // 支持银行
    supportBankHandle() {
      this.supportBankFlag = true;
    },
  },
};
</script>
<style lang="scss">
.v-modal {
  z-index: 300 !important;
}
.supportBank-dialog {
  .el-dialog__header {
    text-align: left;
  }
  .supportBank-content {
    max-height: 700px;
    overflow: auto;
    display: flex;
    flex-wrap: wrap;
    padding-right: 15px;
    &::-webkit-scrollbar {
      width: 3px;
    }
    &::-webkit-scrollbar-track {
      background-color: #e6e6e6;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
    }
    &::-webkit-scrollbar-thumb {
      background-color: #ff7f5b;
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
    }
    .supportBank-item {
      width: 25%;
      box-sizing: border-box;
      border: 1px solid #e6e6e6;
      padding: 15px 0;
      text-align: center;
      margin-right: -1px;
      margin-bottom: -1px;
      &:last-of-type {
        margin-bottom: 0;
      }
      &:nth-last-of-type(2) {
        margin-bottom: 0;
      }
    }
  }
}
</style>

<style lang="scss" scoped>
.bindCard-wrap {
  text-align: center;
  padding: 40px 0 50px 0;
  .el-form {
    width: 50%;
    margin: 0 auto;
    .el-form-item {
      position: relative;
      .supportBank {
        position: absolute;
        font-size: 12px;
        color: #999;
        position: absolute;
        right: 10px;
        text-decoration: underline;
        cursor: pointer;
      }
      .code-btn {
        color: #48b0fa;
        position: absolute;
        right: 10px;
        cursor: pointer;
        &.downNum-btn {
          color: #c0c4cc;
          cursor: default;
          text-decoration: none;
        }
      }
    }
  }
  .el-button {
    width: 345px;
    height: 45px;
    background: #ff7f5b;
    color: #fff;
    margin-top: 30px;
  }
  .successImg {
    width: 100px;
    height: 125px;
    margin-top: 80px;
  }
  .successText {
    font-size: 20px;
    color: #333;
  }
  .footer {
    text-align: left;
    border-top: 1px solid #ebf0f2;
    padding: 25px 40px;
    margin-top: 50px;
    h2 {
      font-size: 16px;
      color: #333;
      font-weight: bold;
      span {
        color: red;
      }
    }
    p {
      color: #999;
    }
  }
}
</style>